<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Banner;
use plugin\admin\app\model\Dict;
use support\Request;
use support\Response;
use plugin\admin\app\model\BannerPosition;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 广告位置 
 */
class BannerPositionController extends Crud
{
    
    /**
     * @var BannerPosition
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new BannerPosition;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('banner-position/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            try {
                $this->checkData($request);
            } catch (\Exception $e) {
                return $this->json(400, $e->getMessage());
            }

            return parent::insert($request);
        }
        return view('banner-position/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            try {
                $this->checkData($request);
            } catch (\Exception $e) {
                return $this->json(400, $e->getMessage());
            }

            return parent::update($request);
        }
        return view('banner-position/update');
    }

    public function get(Request $request): Response
    {
        $positions = BannerPosition::list();

        return $this->json(0, 'ok', $positions);
    }

    protected function checkData($request)
    {
        if ($request->post('name') == '') {
            throw new \Exception('广告位名称不能为空');
        }
    }
}
